package com.goodflower.goodflower;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;

import com.bumptech.glide.signature.StringSignature;
import com.goodflower.goodflower.chang_mac.Change_myflower_image;
import com.goodflower.goodflower.chang_mac.Change_myflower_info;
import com.goodflower.goodflower.info.Info;
import com.goodflower.goodflower.util.HttpUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class FlowerAdapter extends RecyclerView.Adapter<FlowerAdapter.ViewHolder> {

    private Context mcontext;
    private List<Flower> mflowerlist;


    public FlowerAdapter(List<Flower> flower) {
        mflowerlist = flower;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        CardView cardView;
        ImageView flowerImage;
        TextView flowerText;

        public ViewHolder(View itemView) {
            super(itemView);
            cardView = (CardView) itemView;
            flowerImage = (ImageView) itemView.findViewById(R.id.flower_image);
            flowerText = (TextView) itemView.findViewById(R.id.flower_name);
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {

        mcontext = parent.getContext();
        View view = View.inflate(mcontext, R.layout.flower_item, null);
        final ViewHolder viewHolder = new ViewHolder(view);
        viewHolder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mcontext, FlowerSelect.class);
                int adapterPosition = viewHolder.getAdapterPosition();
                Flower flower = mflowerlist.get(adapterPosition);
                intent.putExtra("mac", flower.getFlower_mac());
                intent.putExtra("flower_name", flower.getFlower_name());
                intent.putExtra("username", flower.getFlower_username());
                mcontext.startActivity(intent);
            }
        });
        viewHolder.cardView.setOnLongClickListener(new View.OnLongClickListener() {

            @Override
            public boolean onLongClick(final View v) {
                // Toast.makeText(v.getContext(),"你长按了",Toast.LENGTH_LONG).show();
                final AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
                //builder.setIcon(R.drawable.ic_launcher);
                builder.setTitle("请选择您的操作");
                //    指定下拉列表的显示数据
                final String[] cities = {"删除花园", "修改花园配置", "更改花园配图"};
                //    设置一个下拉的列表选择项
                builder.setItems(cities, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(final DialogInterface dialog, int which) {
                        int adapterPosition = viewHolder.getAdapterPosition();
                        final Flower flower = mflowerlist.get(adapterPosition);
                        switch (which) {
                            case 0:

                                dialog.dismiss();
                                AlertDialog.Builder warn_builder = new AlertDialog.Builder(v.getContext());
                                warn_builder.setTitle("警告！");
                                warn_builder.setMessage("您确定要删除 " + flower.getFlower_name() + " 吗?\n这个操作会删除花园里所有花,并不可恢复！");
                                warn_builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        HttpUtil.sendOkHttpRequest("http://"+ Info.server_ip+"/huahua/delete_mac.php?mac=" + flower.getFlower_mac() + "&username=" + flower.getFlower_username(), new Callback() {
                                            @Override
                                            public void onFailure(Call call, IOException e) {

                                            }

                                            @Override
                                            public void onResponse(Call call, Response response) throws IOException {

                                            }
                                        });
                                        mflowerlist.remove(flower);
                                        notifyDataSetChanged();


                                    }
                                });
                                warn_builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {

                                    }
                                });
                                warn_builder.show();
                                break;
                            case 1:

                                Intent intent = new Intent(mcontext.getApplicationContext(),Change_myflower_info.class);
                                intent.putExtra("flower_mac",flower.getFlower_mac());
                                ((Activity)mcontext).overridePendingTransition(android.R.anim.slide_in_left,android.R.anim.slide_out_right);
                            //    v.getContext().overridePendingTransition(android.R.anim.slide_in_left,android.R.anim.slide_out_right);
                               mcontext.startActivity(intent);

//                                final View view = View.inflate(v.getContext(), R.layout.shezhi_mac, null);
//                                final AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
//                                builder.setTitle("设置");
//                                builder.setView(view);
//                                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
//                                    @Override
//                                    public void onClick(DialogInterface dialog, int which) {
//
//                                    }
//                                });
//                                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
//                                    @Override
//                                    public void onClick(DialogInterface dialog, int which) {
//
//                                    }
//                                });
//                                builder.show();
//                                new Thread(new Runnable() {
//                                    @Override
//                                    public void run() {
//                                        HttpUtil.sendOkHttpRequest("http://"+Info.server_ip+"/huahua/phone_getmacinfo.php?mac=" + flower.getFlower_mac(), new Callback() {
//                                            @Override
//                                            public void onFailure(Call call, IOException e) {
//
//                                            }
//
//                                            @Override
//                                            public void onResponse(Call call, Response response) throws IOException {
//                                                String res = response.body().string();
//                                                try {
//                                                    JSONObject jsonObject = new JSONObject(res);
//                                                    String json_kongshi = jsonObject.getString("kongshi");
//                                                    String json_kongwen = jsonObject.getString("kongwen");
//                                                    String json_jt = jsonObject.getString("jt");
//                                                    String json_pt = jsonObject.getString("pt");
//                                                    String json_macname = jsonObject.getString("macname");
//                                                    Thread.sleep(1000);
//
//                                                    EditText shezhi_kongshi = (EditText) ((Activity) mcontext).findViewById(R.id.shezhi_kongshi);
//                                                    EditText shezhi_kongwen = (EditText)view.findViewById(R.id.shezhi_kongwen);
//                                                    EditText shezhi_jt = (EditText)view.findViewById(R.id.shezhi_jt);
//                                                    EditText shezhi_pt = (EditText)view.findViewById(R.id.shezhi_pt);
//
//                                                    shezhi_kongshi.setText("cao");
//
//                                                  //  shezhi_kongshi.setText(json_kongshi);
//                                                    //shezhi_kongwen.setText(json_kongwen);
//                                                    //shezhi_pt.setText(json_pt);
//                                                    //shezhi_jt.setText(json_jt);
//
//
//                                                } catch (JSONException e) {
//                                                    e.printStackTrace();
//                                                } catch (InterruptedException e) {
//                                                    e.printStackTrace();
//                                                }
//
//                                            }
//                                        });
//                                    }
//                                }).start();
                                break;
                            case 2:
                                Intent image_intent = new Intent(mcontext.getApplicationContext(),Change_myflower_image.class);
                                image_intent.putExtra("flower_mac",flower.getFlower_mac());
                                ((Activity)mcontext).overridePendingTransition(android.R.anim.slide_in_left,android.R.anim.slide_out_right);
                                mcontext.startActivity(image_intent);
                                break;
                            default:
                        }
                    }
                });
                builder.show();
                return true;
            }
        });
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Flower flower = mflowerlist.get(position);
        holder.flowerText.setText(flower.getFlower_name());
        Glide.with(mcontext).load("http://"+Info.server_ip+"/huahua/floweruser/" + flower.getFlower_username() + "/" + flower.getFlower_mac() + ".jpg").signature(new StringSignature(UUID.randomUUID().toString())).into(holder.flowerImage);
    }

    @Override
    public int getItemCount() {
        return mflowerlist.size();
    }


}
